package com.it.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.mapper.StudentMapper;
import com.it.pojo.PageResult;
import com.it.pojo.Student;
import com.it.pojo.StudentQueryParam;
import com.it.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        //1.设置分页参数(PageHelper)
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());

        //2.执行查询
        List<Student> studentList = studentMapper.list(studentQueryParam);

        //3.封装分页结果
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<Student>(p.getTotal(), p.getResult());
    }

    //新增学生接口实现
    @Override
    public void save(Student student) {
        //补 全基础属性
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        //插入学生数据(引导mapper具体实现插入)
        studentMapper.insert(student);
    }

    //根据ID查询学生数据接口实现
    @Override
    public Student getInfo(Integer id) {
        log.info("查询学生ID：{}",id);
        return studentMapper.getById(id);
    }

    //修改学生数据接口实现
    @Override
    public void update(Student student) {
        //补 全基础属性
        student.setUpdateTime(LocalDateTime.now());
        //更新学生数据(引导mapper具体实现更新)
        studentMapper.update(student);
    }

    //删除学生数据接口实现
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    //违纪处理接口实现
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violationById(id,score);
    }
}
